package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanczy.pojo.RoleTable;
import com.wanczy.service.RoleTableService;
import com.wanczy.service.impl.RoleTableServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/FindRoleServlet02")
public class FindRoleServlet02 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		String name = "";
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent"));
		
		
		RoleTableService service = new RoleTableServiceImpl();
		List<RoleTable> list = service.find( name, pageSize,pageCurrent);
		int count = service.findCount(name);
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("count", count);
		jsonArray.add(jsonObject);
		Iterator<RoleTable> it = list.iterator();
		while(it.hasNext()) {
			RoleTable role = it.next();
			JSONObject jsonO = new JSONObject();
			jsonO.put("roleId", role.getRole_id());
			jsonO.put("roleName", role.getRole_name());
			jsonO.put("roleDescribe", role.getRole_describe());
			jsonO.put("roleState", role.getRole_state());
			jsonArray.add(jsonO);
		}
		out.println(jsonArray.toString());
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
